<?php
/**
 * @link https://ym2.cleverstone.top
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace common\models;

/**
 * 地区服务类
 * @auther 荒
 * @since 1.0.0
 * @datetime 2023.10.25 17:00
 */
class RegionService
{
    /**
     * 数据列
     * @param int $pid 父ID
     * @return array
     */
    public static function getColumnsByPid($pid)
    {
        return Region::query(['name'])
            ->where(['pid' => $pid, 'status' => 1])
            ->orderBy([
                'sort' => SORT_DESC,
                'id' => SORT_ASC,
            ])
            ->indexBy('name')
            ->column();
    }

    /**
     * 级联动
     * @param int $pid 父ID
     * @return array
     */
    public static function getCascadeByPid($pid)
    {
        $all = Region::query(['name', 'status'])
            ->where(['pid' => $pid])
            ->orderBy([
                'sort' => SORT_DESC,
                'id' => SORT_ASC,
            ])
            ->all();

        $result = [];
        foreach ($all as $item) {
            $result[] = [
                'value' => $item['name'],
                'text' => $item['name'],
                'disabled' => !$item['status'],
            ];
        }

        return $result;
    }
}